<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="政策名称" prop="policyName">
				<el-input v-model="form.policyName" placeholder="政策名称" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="政策类型" prop="policyType">
						<el-select v-model="form.policyType" clearable filterable style="width: 100%">
							<el-option v-for="item in policyTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="政策状态" prop="policyStatus">
						<el-select v-model="form.policyStatus" clearable filterable style="width: 100%">
							<el-option v-for="item in policyStatusList" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="生效时间" prop="effectTime">
						<el-date-picker v-model="form.effectTime" placeholder="生效时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
				</el-col>
			</el-row>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'crm'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="政策内容" prop="policyContent" style="margin-top: 10px;">
				<el-input type="textarea" rows="4" v-model="form.policyContent" placeholder="政策内容" clearable maxlength="380"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>

import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg:false,
			loading:false,
			fileList:[],
			productOptions:[],
			mx:[],
			addTemplate: {
				productId: '',
				productName:"",
				materialCode: '',
				model: '',
				count: 0,
				unit:"",
				guidePrice:0,
				delivery:"",
				remark: "",
			},
			mode: "add",
			apiObj:this.$API.file.attach.uploadFile,
			titleMap: {
				add: '创建招商政策',
				edit: '编辑招商政策',
			},
			form: {
				id:'',
				policyName:'',
				policyType:"",
				policyStatus: '',
				effectTime: '',
				policyContent: '',
				attachId:''
			},
			policyTypeList:[{"label":"人才落户","value":1},{"label":"房租减税","value":2},{"label":"装修补贴","value":3},{"label":"税费减免","value":4}]
			,policyStatusList:[{"label":"有效","value":1},{"label":"失效","value":2}]
			,options: [],
			quotationOptions:[],
			rules: {
				policyName: [
					{required: true, message: '政策名称不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.policy.policyType.savePolicyType.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.policy.policyType.updatePolicyType.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
		setData(data) {
			this.form = data
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
